"""
Case Type   : 资源池化
Case Name   : 扩容指定超时时间
Create At   : 2024.10
Owner       : chen
Description :
    1.指定超时时间为-1
    gs_expansion -U {user} -G {user} -X {xml} -h {ip1} --time-out -1
    2.指定超时时间为0
    gs_expansion -U {user} -G {user} -X {xml} -h {ip1} --time-out 0
    3.指定超时时间为300
    gs_expansion -U {user} -G {user} -X {xml} -h {ip1} --time-out 300
    4.恢复环境
Expect      :
    1.合理报错
    2.合理报错
    3.扩容成功
    4.恢复环境
"""

import os
import unittest
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.sta2_node = Node('Standby2DbUser')
        self.db_host = self.sta2_node.db_host
        self.s_user = self.pri_node.ssh_user

    def test_shared_storage(self):
        text = '-----step1: 指定超时时间为-1 expect: 合理报错-----'
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        cmd = f'''source {macro.DB_ENV_PATH}; 
            gs_expansion -U {self.s_user} -G {self.s_user} -X {xml_path} -h {self.db_host} --time-out -1'''
        self.log.info(cmd)
        res = self.pri_root_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(f'''The parameter '--time-out' type should be a nonnegative integer''', res)

        text = '-----step2: 指定超时时间为0 expect: 合理报错-----'
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH}; gs_expansion \
            -U {self.s_user} -G {self.s_user} -X {xml_path} -h {self.db_host} --time-out 0'''
        self.log.info(cmd)
        res = self.pri_root_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(f'''The parameter '--time-out' value is incorrect''', res)

        text = f'-----step3: 指定超时时间为300 expect: 成功-----'
        self.log.info(text)
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.s_user} \
            -G {self.s_user} -X {xml_path} -h {self.db_host} --time-out 300'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 300
            spawn {cmd}
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*assword*:"
            send "{self.pri_root_node.ssh_password}\\n"
            expect "*lease repeat for database*:"
            send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)

    def tearDown(self):
        text = "-----step4: 恢复环境 expect: 成功-----"
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
            expect <<EOF
            set timeout 600
            spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
            expect "*drop the target node (yes/no)?*"
            send "yes\\n"
            expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        res = self.sta2_node.sh(f'source {macro.DB_ENV_PATH}; ' \
            'gs_uninstall --delete-data').result()
        self.log.info(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')